package com.unitd.modules.gen.entity;

import com.google.common.collect.Lists;
import com.unitd.comm.entity.SysEntity;
import com.unitd.frame.comm.utils.StringUtils;
import com.unitd.modules.sys.entity.SysDict;
import org.hibernate.validator.constraints.Length;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.List;

/**
 * @desc 生成代码的业务表Entity, 用于定义该表结构的对象信息
 * @filename GenTable.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0 
 * @date 2017-04-29
 */ 
@Table(name = "gen_table")
public class GenTable extends SysEntity {

	/** 表分类类型,根据sys_dict参数表来配置类型信息,页面下拉显示,选择 */
	@Column(name = "category_value")
	private String categoryValue;
	/** 表分类类型名称 */
	@Column(name = "category_name")
	private String categoryName;
	/** 表名 */
	@Column(name = "table_name")
	private String tableName;
	/** 描述 */
	private String comments;
	/** 实体类名称(类名(第一个字母大写)，如：sys_user => SysUser) */
	@Column(name = "class_name")
	private String className;
	/** 是否创建对应的FO(0:不创建; 1:创建, 默认不创建) */
	@Column(name = "create_fo")
	private String createFo;
	/** 是否创建对应的AR表(0:不创建; 1:创建, 默认不创建) */
	@Column(name = "create_ar")
	private String createAr;
	/** 关联父表 */
	@Column(name = "parent_table")
	private String parentTable;
	/** 关联父表外键 */
	@Column(name = "parent_table_fk")
	private String parentTableFk;

	@Transient
	private GenTableColumn pk; 												// 表的主键
	@Transient
	private List<GenTableColumn> columnList = Lists.newArrayList(); 		// 表列
	@Transient
	private List<GenTableColumn> transColumnList = Lists.newArrayList(); 	// 表的列中,与数据库映射但是与数据库字段不一致的列
	@Transient
	private List<String> pkList; 											// 当前表主键列表
	@Transient
	private String nameLike; 												// 按名称模糊查询
	@Transient
	private GenTable parent; 												// 父表对象
	@Transient
	private List<GenTable> childList = Lists.newArrayList(); 				// 子表列表
	@Transient
	private SysDict tableCategory; 											// 数据表所属的表类型

	/**
	 * @desc 构造方法 GenTable
	 */
	public GenTable(String id, String categoryValue, String categoryName, String tableName, String comments, String className, String createFo, String createAr, String parentTable, String parentTableFk, String createBy, java.util.Date createDate, String updateBy, java.util.Date updateDate, String remarks, String delFlag) {
		this.id = id;

		this.categoryValue = categoryValue;
		this.categoryName = categoryName;
		this.tableName = tableName;
		this.comments = comments;
		this.className = className;
		this.createFo = createFo;
		this.createAr = createAr;
		this.parentTable = parentTable;
		this.parentTableFk = parentTableFk;
		this.createBy = createBy;
		this.createDate = createDate;
		this.updateBy = updateBy;
		this.updateDate = updateDate;
		this.remarks = remarks;
		this.delFlag = delFlag;
	}

	public GenTable(String id) {
		super(id);
	}

	/**
	 * @desc 构造方法 GenTable
	 */
	public GenTable() {
		super();
	}

	@Length(min = 1)
	public String getCategoryValue() {
		return categoryValue;
	}

	public void setCategoryValue(String categoryValue) {
		this.categoryValue = categoryValue;
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	/**
	 * @desc 获取表名
	 * @return table_name - 表名
	 */
	@Length(min = 1, max = 200)
	public String getTableName() {
		return tableName;
	}

	/**
	 * @desc 设置表名
	 * @param tableName 表名
	 */
	public void setTableName(String tableName) {
		this.tableName = tableName == null ? null : tableName.trim();
	}

	/**
	 * @desc 获取描述
	 * @return comments - 描述
	 */
	@Length(min = 1, max = 300)
	public String getComments() {
		return comments;
	}

	/**
	 * @desc 设置描述
	 * @param comments 描述
	 */
	public void setComments(String comments) {
		this.comments = comments == null ? null : comments.trim();
	}

	/**
	 * @desc 获取实体类名称
	 * @return class_name - 实体类名称
	 */
	@Length(min = 1, max = 50)
	public String getClassName() {
		return className;
	}

	/**
	 * @desc 设置实体类名称
	 * @param className 实体类名称
	 */
	public void setClassName(String className) {
		this.className = className == null ? null : className.trim();
	}

	/**
	 * @desc 获取是否创建对应的FO表(0:不创建; 1:创建, 默认不创建)
	 * @return createfo-是否创建对应的FO表(0:不创建; 1:创建, 默认不创建)
	 */
	public String getCreateFo() {
		return createFo;
	}

	/**
	 * @desc 设置是否创建对应的FO表(0:不创建; 1:创建, 默认不创建)
	 * @param createFo 是否创建对应的FO表
	 */
	public void setCreateFo(String createFo) {
		this.createFo = createFo;
	}
	/**
	 * @desc 获取是否创建对应的AR表(0:不创建; 1:创建, 默认不创建)
	 * @return createAr-是否创建对应的AR表(0:不创建; 1:创建, 默认不创建)
	 */
	public String getCreateAr() {
		return createAr;
	}

	/**
	 * @desc 设置是否创建对应的AR表(0:不创建; 1:创建, 默认不创建)
	 * @param createAr 是否创建对应的AR表
	 */
	public void setCreateAr(String createAr) {
		this.createAr = createAr;
	}

	/**
	 * @desc 获取关联父表
	 * @return parent_table - 关联父表
	 */
	public String getParentTable() {
		return parentTable;
	}

	/**
	 * @desc 设置关联父表
	 * @param parentTable 关联父表
	 */
	public void setParentTable(String parentTable) {
		this.parentTable = parentTable == null ? null : parentTable.trim();
	}

	/**
	 * @desc 获取关联父表外键
	 * @return parent_table_fk - 关联父表外键
	 */
	public String getParentTableFk() {
		return parentTableFk;
	}

	/**
	 * @desc 设置关联父表外键
	 * @param parentTableFk 关联父表外键
	 */
	public void setParentTableFk(String parentTableFk) {
		this.parentTableFk = parentTableFk == null ? null : parentTableFk.trim();
	}

	public GenTableColumn getPk() {
		return pk;
	}

	public void setPk(GenTableColumn pk) {
		this.pk = pk;
	}

	public List<GenTableColumn> getColumnList() {
		return columnList;
	}

	public void setColumnList(List<GenTableColumn> columnList) {
		this.columnList = columnList;
	}

	public List<GenTableColumn> getTransColumnList() {
		return transColumnList;
	}

	public void setTransColumnList(List<GenTableColumn> transColumnList) {
		this.transColumnList = transColumnList;
	}

	public String getNameLike() {
		return nameLike;
	}

	public void setNameLike(String nameLike) {
		this.nameLike = nameLike;
	}

	public List<String> getPkList() {
		return pkList;
	}

	public void setPkList(List<String> pkList) {
		this.pkList = pkList;
	}

	public GenTable getParent() {
		return parent;
	}

	public void setParent(GenTable parent) {
		this.parent = parent;
	}

	public List<GenTable> getChildList() {
		return childList;
	}

	public void setChildList(List<GenTable> childList) {
		this.childList = childList;
	}

	public SysDict getTableCategory() {
		return tableCategory;
	}

	public void setTableCategory(SysDict tableCategory) {
		this.tableCategory = tableCategory;
	}

	/**
	 * @desc 获取列名和说明
	 * @return
	 */
	public String getNameAndComments() {
		return getTableName() + (comments == null ? "" : "  :  " + comments);
	}

	/**
	 * @desc 获取导入依赖包字符串
	 * @return
	 */
	public List<String> getImportList() {
		// 引用列表
		List<String> importList = Lists.newArrayList();
		for (GenTableColumn column : getColumnList()) {
			if (column.getIsNotBaseField() || ("1".equals(column.getIsQuery()) && "between".equals(column.getQueryType())
					&& ("createDate".equals(column.getSimpleJavaField()) || "updateDate".equals(column.getSimpleJavaField())))) {
				// 导入类型依赖包， 如果类型中包含“.”，则需要导入引用。
				if (StringUtils.indexOf(column.getJavaType(), ".") != -1 && !importList.contains(column.getJavaType())) {
					importList.add(column.getJavaType());
				}
			}
			if (column.getIsNotBaseField()) {
				// 导入JSR303、Json等依赖包
				for (String ann : column.getAnnotationList()) {
					if (!importList.contains(StringUtils.substringBeforeLast(ann, "("))) {
						importList.add(StringUtils.substringBeforeLast(ann, "("));
					}
				}
			}
		}
		// 如果有子表，则需要导入List相关引用
		if (getChildList() != null && getChildList().size() > 0) {
			if (!importList.contains("java.util.List")) {
				importList.add("java.util.List");
			}
			if (!importList.contains("com.google.common.collect.Lists")) {
				importList.add("com.google.common.collect.Lists");
			}
		}
		return importList;
	}

	/**
	 * @desc 是否存在父类
	 * @return
	 */
	public Boolean getParentExists() {
		return parent != null && StringUtils.isNotBlank(parentTable) && StringUtils.isNotBlank(parentTableFk);
	}

	/**
	 * @desc 是否存在create_date列
	 * @return
	 */
	public Boolean getCreateDateExists() {
		for (GenTableColumn c : columnList) {
			if ("create_date".equals(c.getColumnName())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @desc 是否存在update_date列
	 * @return
	 */
	public Boolean getUpdateDateExists() {
		for (GenTableColumn c : columnList) {
			if ("update_date".equals(c.getColumnName())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @desc 是否存在del_flag列
	 * @return
	 */
	public Boolean getDelFlagExists() {
		for (GenTableColumn c : columnList) {
			if ("del_flag".equals(c.getColumnName())) {
				return true;
			}
		}
		return false;
	}
}